package client

import (
	"context"
	"google.golang.org/grpc"
	"hotel-app/common/proto/hotel"
)

type Handler func(ctx context.Context, client hotel.HotelClient) (interface{}, error)

func HotelClient(ctx context.Context, handler Handler) (interface{}, error) {

	dial, err := grpc.Dial("127.0.0.1:8082", grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := hotel.NewHotelClient(dial)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	return res, nil
}
func SendSms(ctx context.Context, in *hotel.SendSmsReq) (*hotel.SendSmsResp, error) {
	client, err := HotelClient(ctx, func(ctx context.Context, client hotel.HotelClient) (interface{}, error) {
		sms, err := client.SendSms(ctx, in)
		if err != nil {
			return nil, err
		}
		return sms, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*hotel.SendSmsResp), nil
}
func UserLogin(ctx context.Context, in *hotel.UserLoginReq) (*hotel.UserLoginResp, error) {
	client, err := HotelClient(ctx, func(ctx context.Context, client hotel.HotelClient) (interface{}, error) {
		login, err := client.UserLogin(ctx, in)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*hotel.UserLoginResp), nil
}
func OrderList(ctx context.Context, in *hotel.OrderListReq) (*hotel.OrderListResp, error) {
	client, err := HotelClient(ctx, func(ctx context.Context, client hotel.HotelClient) (interface{}, error) {
		list, err := client.OrderList(ctx, in)
		if err != nil {
			return nil, err
		}
		return list, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*hotel.OrderListResp), nil
}
func OrderListByUser(ctx context.Context, in *hotel.OrderListByUserReq) (*hotel.OrderListByUserResp, error) {
	client, err := HotelClient(ctx, func(ctx context.Context, client hotel.HotelClient) (interface{}, error) {
		list, err := client.OrderListByUser(ctx, in)
		if err != nil {
			return nil, err
		}
		return list, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*hotel.OrderListByUserResp), nil
}
